package com.food.eat.controller;

import com.food.eat.pojo.*;
import com.food.eat.pojo.bo.ShopCatBo;
import com.food.eat.pojo.vo.CategoryVo;
import com.food.eat.pojo.vo.CommentLevelCountVo;
import com.food.eat.pojo.vo.ItemsVo;
import com.food.eat.service.CarouselService;
import com.food.eat.service.CategoryService;
import com.food.eat.service.ItemCommentService;
import com.food.eat.service.ItemsService;
import com.food.eat.util.FoodResultJson;
import com.food.eat.util.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.food.eat.controller.BaseController.COMMENT_PAGE_SIZE;

/**
 * @Author:RenPu
 * @Date: 2020/7/20 17:11
 * @Version: 1.0
 * @description:
 */

@Api(value = "商品接口", tags = {"商品接口信息展示的相关接口"})
@RestController
@RequestMapping("/items")
public class ItemsController extends BaseController {

    @Resource
    private ItemsService itemsService;


    @Resource
    private ItemCommentService itemCommentService;


    @ApiOperation(httpMethod = "GET", value = "查询商品详情", notes = "查询商品详情")
    @GetMapping("/info/{itemId}")
    public FoodResultJson carousel(
            @ApiParam(name = "itemId", value = "商品的id值", required = true)
            @PathVariable String itemId) {

        if (StringUtils.isBlank(itemId)) {
            return FoodResultJson.errMap("商品的id值为空o(╥﹏╥)o！！！！！");
        }

        Items items = itemsService.queryByOneItems(itemId);
        ItemsParam itemsParam = itemsService.queryByOneItemsParam(itemId);
        List<ItemsImg> itemsImgs = itemsService.queryManyItemsImg(itemId);
        List<ItemsSpec> itemsSpecs = itemsService.queryManyItemsSpec(itemId);

        ItemsVo itemsVo = new ItemsVo();
        itemsVo.setItem(items);
        itemsVo.setItemImgList(itemsImgs);
        itemsVo.setItemParams(itemsParam);
        itemsVo.setItemSpecList(itemsSpecs);
        return FoodResultJson.ok(itemsVo);
    }


    @ApiOperation(httpMethod = "GET", value = "查询商品评价等级数量", notes = "查询商品评价等级数量")
    @GetMapping("/commentLevel")
    public FoodResultJson commentLevel(
            @ApiParam(name = "itemId", value = "商品的id值", required = true)
            @RequestParam String itemId) {

        if (StringUtils.isBlank(itemId)) {
            return FoodResultJson.errMap("商品的id值为空o(╥﹏╥)o！！！！！");
        }
        CommentLevelCountVo commentLevelCountVo = itemsService.queryCommentsCount(itemId);
        return FoodResultJson.ok(commentLevelCountVo);
    }


    /**
     * PathVariable：注解主要
     * RequestParam
     */

    @ApiOperation(httpMethod = "GET", value = "查询用户评论", notes = "查询用户评论")
    @GetMapping("/comments")
    public FoodResultJson comments(
            @ApiParam(name = "itemId", value = "商品的id值", required = true)
            @RequestParam String itemId,
            @ApiParam(name = "page", value = "查询页数的值", required = true)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "每页展示的数据的条数", required = true)
            @RequestParam Integer pageSize,
            @ApiParam(name = "level", value = "评价等级", required = true)
            @RequestParam Integer level
    ) {

        if (itemId == null) {
            return FoodResultJson.errMsg(null);
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {

            pageSize = COMMENT_PAGE_SIZE;
        }

        PagedGridResult pagedGridResult = itemCommentService.queryByItemsIdCommentInfo(level, itemId, page, pageSize);

        return FoodResultJson.ok(pagedGridResult);
    }


    /**
     * PathVariable：注解主要
     * RequestParam
     */

    @ApiOperation(httpMethod = "GET", value = "首页商品查询接口实现", notes = "首页商品查询接口实现")
    @GetMapping("/search")
    public FoodResultJson search(
            @ApiParam(name = "keywords", value = "商品的id值", required = true)
            @RequestParam String keywords,
            @ApiParam(name = "page", value = "查询页数的值", required = true)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "每页展示的数据的条数", required = true)
            @RequestParam Integer pageSize,
            @ApiParam(name = "sort", value = "评价等级", required = true)
            @RequestParam String sort
    ) {

        if (StringUtils.isBlank(keywords)) {
            return FoodResultJson.errMsg(null);
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {

            pageSize = PAGE_SIZE;
        }

        PagedGridResult pagedGridResult = itemCommentService.searchItemsMessageInfo(keywords, sort, page, pageSize);

        return FoodResultJson.ok(pagedGridResult);
    }


    @ApiOperation(httpMethod = "GET", value = "首页商品查询接口实现", notes = "首页商品查询接口实现")
    @GetMapping("/catItems")
    public FoodResultJson catItems(
            @ApiParam(name = "catId", value = "商品的id值", required = true)
            @RequestParam Integer catId,
            @ApiParam(name = "page", value = "查询页数的值", required = true)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "每页展示的数据的条数", required = true)
            @RequestParam Integer pageSize,
            @ApiParam(name = "sort", value = "评价等级", required = true)
            @RequestParam String sort
    ) {

        if (catId == null) {
            return FoodResultJson.errMsg(null);
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {

            pageSize = PAGE_SIZE;
        }

        PagedGridResult pagedGridResult = itemCommentService.searchItemsByCatIdType(catId, sort, page, pageSize);

        return FoodResultJson.ok(pagedGridResult);
    }


    //用于用户长时间未登录网站，刷新购物车的数据（主要是商品的价格），类似京东，淘宝
    @ApiOperation(httpMethod = "GET", value = "购物车刷新显示接口", notes = "购物车刷新显示接口")
    @GetMapping("/refresh")
    public FoodResultJson refresh(
            @ApiParam(name = "itemSpecIds", value = "拼接的规格ids", required = true)
            @RequestParam String itemSpecIds
    ) {

        if (StringUtils.isBlank(itemSpecIds)) {
            FoodResultJson.errMsg(null);
        }

        List<ShopCatBo> shopCatBos = itemCommentService.queryItemsBySpecId(itemSpecIds);

        return FoodResultJson.ok(shopCatBos);
    }


}
